//Copyright (c) 2012, peter.ducai@gmail.com
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following 
//conditions are met:
//
//Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
//in the documentation and/or other materials provided with the distribution.
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
//INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
//OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
//OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
//OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
//OF SUCH DAMAGE.
package com.daemonna.fluentlee_core.objects;

import com.daemonna.fluentlee_core.database.*;
import com.daemonna.fluentlee_core.job.job;
import com.daemonna.fluentlee_core.pools.schedule_pool;

/*
 * @author peter 'daemonna' ducai @author peter.ducai@gmail.com @author
 * http://code.google.com/p/fluentlee/
 */
public class connector {

    public enum type {

        DERBY, SQLITE, ORACLE, MYSQL, POSTGRES, UNDEFINED
    }

    public enum status {

        UNCREATED, INNACTIVE, ACTIVE, CLOSED
    }
    public String name = null;
    public type conn_type = type.UNDEFINED;
    public String url = null;
    public String user = null;
    public String password = null;
    public int id = 0;
    public status stats = status.UNCREATED;
    private db_derby derbydb;
    private db_mysql mysqldb;
    private db_oracle oracledb;
    private db_postgres postgdb;
    private db_sqlite sqlitedb;

    public connector() {
        stats = status.INNACTIVE;
    }

    /*
     * format url by type of db
     */
    public void format_url_by_type() throws Exception {

        //check type and format string
        if (this.conn_type == type.SQLITE) {
            url = "jdbc:sqlite:" + url;
        } else if (this.conn_type == type.ORACLE) {
            url = "jdbc:oracle:thin:@" + url;
        } else if (this.conn_type == type.POSTGRES) {
            url = "jdbc:postgresql://" + url;
        } else if (this.conn_type == type.MYSQL) {
            url = "jdbc:mysql://" + url;
        } else if (this.conn_type == type.DERBY) {
            url = "jdbc:derby:testdb;create=true" + url;
        } else {
            throw new Exception("no such connector type");
        }
    }

    /*
     * init connector by db type
     */
    public void init_connector() throws Exception {

        /*
         * if status is other than active, create new connection of some type
         */
        if (this.stats != status.ACTIVE) {

            this.stats = status.ACTIVE;
            //this.format_url_by_type();

            /*
             * depending on type, create connector
             */
            if (this.conn_type == type.SQLITE) {

                sqlitedb = new db_sqlite() {
                };
                sqlitedb.init_db(this);

            } else if (this.conn_type == type.ORACLE) {

                oracledb = new db_oracle() {
                };
                oracledb.init_db(this);

            } else if (this.conn_type == type.POSTGRES) {

                postgdb = new db_postgres() {
                };
                postgdb.init_db(this);

            } else if (this.conn_type == type.MYSQL) {

                mysqldb = new db_mysql() {
                };
                mysqldb.init_db(this);

            } else if (this.conn_type == type.DERBY) {

                derbydb = new db_derby() {
                };
                derbydb.init_db(this);

            } else {
                throw new Exception("no such connector type :from: init_connector()");
            }


        }
    }

    /*
     * execute sql
     */
    public void execute_sql(String sql, int sched_index, int x, int v) throws Exception {
        sql = sql.toLowerCase();

        /*
         * depending on type, execute sql
         */
        if (this.conn_type == type.SQLITE) {

            sqlitedb.init_db(this);

            if (sql.indexOf("select count(*)") != -1) {
                System.out.println("EXECUTING select");
                sqlitedb.execute_sql_query(sql, sched_index, x, v);
            } else {
                System.out.println("EXECUTING something else");
                sqlitedb.execute_sql(sql);
                schedule_pool.getInstance().scheds[sched_index].suites[x].jobs[v].result = job.jobresult.PASS;
            }


        } else if (this.conn_type == type.ORACLE) {

            oracledb.init_db(this);

            if (sql.indexOf("select count(*)") != -1) {
                System.out.println("EXECUTING select");
                oracledb.execute_sql_query(sql, sched_index, x, v);
            } else {
                System.out.println("EXECUTING something else");
                oracledb.execute_sql(sql);
                schedule_pool.getInstance().scheds[sched_index].suites[x].jobs[v].result = job.jobresult.PASS;
            }


        } else if (this.conn_type == type.POSTGRES) {

            postgdb.init_db(this);

            if (sql.indexOf("select count(*)") != -1) {
                System.out.println("EXECUTING select");
                postgdb.execute_sql_query(sql, sched_index, x, v);
            } else {
                System.out.println("EXECUTING something else");
                postgdb.execute_sql(sql);
                schedule_pool.getInstance().scheds[sched_index].suites[x].jobs[v].result = job.jobresult.PASS;
            }


        } else if (this.conn_type == type.MYSQL) {

            mysqldb.init_db(this);

            if (sql.indexOf("select count(*)") != -1) {
                System.out.println("EXECUTING select");
                mysqldb.execute_sql_query(sql, sched_index, x, v);
            } else {
                System.out.println("EXECUTING something else");
                mysqldb.execute_sql(sql);
                schedule_pool.getInstance().scheds[sched_index].suites[x].jobs[v].result = job.jobresult.PASS;
            }


        } else if (this.conn_type == type.DERBY) {

            derbydb.init_db(this);

            if (sql.indexOf("select count(*)") != -1) {
                System.out.println("EXECUTING select");
                derbydb.execute_sql_query(sql, sched_index, x, v);
            } else {
                System.out.println("EXECUTING something else");
                derbydb.execute_sql(sql);
                schedule_pool.getInstance().scheds[sched_index].suites[x].jobs[v].result = job.jobresult.PASS;
            }
            //schedule_pool.getInstance().scheds[sched_index].suites[x].jobs[v].result = job.jobresult.PASS;
        }

        
    }
}
